/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.gson;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 *
 * @author jeovane
 */
public class ManipulaGson {
    private int id;
    private String name;
    private String userName;
    private String email;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getUserName() {
        return userName;
    }

    public String getEmail() {
        return email;
    }
    
//    @Override
//    public String toString(){
//        return "{\n" +
//                "  user: {\n" +
//                "    id:" + getId() + ",\n" +
//                "    name: " + getName() + ",\n" +
//                "    username:" + getUserName() + ",\n" +
//                "    email: " + getEmail() + "\n" +
//                "  }\n" +
//                "}";
//    }
        
    
    public ManipulaGson loadUserFromJSON(String jsonText) {
        Gson gson = new Gson();
        ManipulaGson user = gson.fromJson(jsonText, ManipulaGson.class);
        return user;
    }
    
    public static void main(String[] args) {
//        ManipularGson manipularGson = new ManipularGson();
//        manipularGson.setId(1);
//        manipularGson.setName("Jeovane Reges");
//        manipularGson.setEmail("jeovane.reges@gmail.com");
//        manipularGson.setUserName("je0v4ne");
//        
        Gson gson = new Gson();
//        String userJSONString = gson.toJson(manipularGson);
//        
//        System.out.println("userJSONString: " + userJSONString);       
        
//        String jsonText = "{\n" +
//                            "  user: {\n" +
//                            "    id: 123456,\n" +
//                            "    name: \"Neto Marin\",\n" +
//                            "    username: \"netomarin\",\n" +
//                            "    email: \"netomarin@globalcode.com.br\"\n" +
//                            "  }\n" +
//                            "}";
//        
//        String jsonText = "{\n" + 
//                "id:123, \n" +
//                "firstName:Wektabyte \n" +
//                "}";
        
        String jsonText = "{" +
                   "  user: {" +
                   "    id: 1," +
                   "    name: Jeovane" +
                   "  }" +
                   "}";
        
//        Type collectionType = new TypeToken<List<ManipulaGson>>(){}.getType();
//        List<ManipulaGson> details = gson.fromJson(jsonText, collectionType);
//        
//        for (ManipulaGson mg : details) {
//            System.out.println(mg.getId());
//        }
        
//        ManipulaGson mg = gson.fromJson(jsonText, ManipulaGson.class);
//        System.out.println(mg.getId());
        
        Map<String, Object> jsonNodes = gson.fromJson(jsonText, Map.class);
        String innerJson = gson.toJson(jsonNodes.get("user"));
        ManipulaGson mg = gson.fromJson(innerJson, ManipulaGson.class);
        mg.setName("José Jeovane");
        
        System.out.println(gson.toJson(mg));
        
    }
}
